package com.example.a28796.beautiful.styAdapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.a28796.beautiful.R;
import com.example.a28796.beautiful.stybean.HotBean;
import com.squareup.picasso.Picasso;

import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.List;

/**
 * Created by lenovo on 2016/8/30.
 */
public class ReHotAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<HotBean.ObjBean.ListDataBean> mData;
    private Context context;
    private ImageOptions options;
    private static final  int VIEW_TYPE0 = 0;
    private static final  int VIEW_TYPE1 = 1;
    public ReHotAdapter(List<HotBean.ObjBean.ListDataBean> mData, Context context) {
        this.mData = mData;
        this.context = context;
        options = new ImageOptions.Builder().setCircular(true).setUseMemCache(true).build();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType==VIEW_TYPE0) {
            View view = LayoutInflater.from(context).inflate(R.layout.item_hot, null);
            return new MyViewHolder(view);
        }else if(viewType == VIEW_TYPE1){
            View view = LayoutInflater.from(context).inflate(R.layout.item_hot_two, null);
            return new MyViewHolders(view);
        }else{
            return null;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        HotBean.ObjBean.ListDataBean dataBean = mData.get(position);
        int itemViewType = getItemViewType(position);
        switch (itemViewType){
            case VIEW_TYPE0:
                ((MyViewHolder)holder).title.setText(dataBean.getTitle());
                ((MyViewHolder)holder).content.setText(dataBean.getSummary());
                ((MyViewHolder)holder).userName.setText(dataBean.getUser().getNickname());
                ((MyViewHolder)holder).pager.setText("共"+dataBean.getArticleGroupNum().getArticleNum()+"篇");
                ((MyViewHolder)holder).count.setText(dataBean.getArticleGroupNum().getViewNum()/10000+"万+浏览");
                x.image().bind(((MyViewHolder)holder).userIcon,dataBean.getUser().getHeadUrl(),options);
                if(dataBean.getPhotoes()!=null&&dataBean.getPhotoes().size()!=0){
                    Picasso.with(context).load(dataBean.getPhotoes().get(0).getUrl()).into(((MyViewHolder)holder).userPic);
                }
                break;
            case VIEW_TYPE1:
                ((MyViewHolders)holder).tvTitle.setText(dataBean.getTitle());
                ((MyViewHolders)holder).tvContent.setText(dataBean.getSummary());
                ((MyViewHolders)holder).tvName.setText(dataBean.getUser().getNickname());
                ((MyViewHolders)holder).tvCount.setText(dataBean.getArticleGroupNum().getViewNum()/10000+"万+浏览");
                x.image().bind(((MyViewHolders)holder).ivIcon,dataBean.getUser().getHeadUrl(),options);
                if(dataBean.getPhotoes()!=null&&dataBean.getPhotoes().size()!=0){
                    Picasso.with(context).load(dataBean.getPhotoes().get(0).getUrl()).into(((MyViewHolders)holder).ivPic);
                }
                break;
        }


    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView userIcon,userPic;
        private TextView title,content,userName,pager,count;
        public MyViewHolder(View view) {
            super(view);
            userIcon = (ImageView) view.findViewById(R.id.iv_item_hot_icon);
            userPic = (ImageView) view.findViewById(R.id.iv_item_hot_show);
            title = (TextView) view.findViewById(R.id.tv_item_hot_title);
            content = (TextView) view.findViewById(R.id.tv_item_hot_content);
            userName = (TextView) view.findViewById(R.id.tv_item_hot_username);
            pager = (TextView) view.findViewById(R.id.tv_item_hot_pager);
            count = (TextView) view.findViewById(R.id.tv_item_hot_count);
        }
    }

    public static class MyViewHolders extends RecyclerView.ViewHolder{
        private ImageView ivPic,ivIcon;
        private TextView tvName,tvTitle,tvCount,tvContent;
        public MyViewHolders(View itemView) {
            super(itemView);
            ivIcon = (ImageView) itemView.findViewById(R.id.iv_hot_two_icon);
            ivPic = (ImageView) itemView.findViewById(R.id.iv_hot_two_pic);

            tvCount = (TextView) itemView.findViewById(R.id.tv_hot_two_content);
            tvCount = (TextView) itemView.findViewById(R.id.tv_hot_two_count);
            tvName = (TextView) itemView.findViewById(R.id.tv_hot_two_name);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_hot_two_title);
            tvContent = (TextView) itemView.findViewById(R.id.tv_hot_two_content);

        }
    }

    @Override
    public int getItemViewType(int position) {

       if(mData.get(position).getUser().getNickname().equals("社区站长")){
            return 1;
        }else{
            return 0;
        }

    }
}
